package com.wing.ext.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wing.common.Constants;
import com.wing.common.util.GUID;
import com.wing.common.util.PageBean;
import com.wing.common.util.ScaleImage;
import com.wing.ext.domain.Products;
import com.wing.ext.service.ProductsManager;

/**
 * 
 * 
 * @author wyf
 * 
 */
 
@Controller
@RequestMapping(value = "/ext/products")
public class ProductsController {

	@Autowired
	private ProductsManager productsManager;
	
	/**
	 * 获得列表数据
	 *  
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String list() {
		return "/ext/products/productsIndex";
	}
	
	@RequestMapping(value = "get/{url}", method = RequestMethod.GET)
	public String delete(@PathVariable String url) {
		return "/ext/products/"+url;
	}
		
	@RequestMapping(value = "list", method = RequestMethod.POST)
	public @ResponseBody PageBean list(HttpServletRequest request,int currentPage, String pageSize,String groupId) {
		int _iPageSize = 0;
		if (pageSize == null) {
			_iPageSize = PageBean.DEFAULT_PAGESIZE;
		} else {
			_iPageSize= Integer.parseInt(pageSize);
		}
		return  productsManager.findPageList(currentPage, _iPageSize,groupId,(String)request.getSession().getAttribute(Constants.SESSION_SITE_ID));
	}
	
	/**
	 * 打开编辑页面
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "toForm/{id}", method = RequestMethod.GET)
	public String toEditPage(@PathVariable String id, String groupId,Model model) {
		Products products = null;
		if(!id.equals("0")){
			products = productsManager.findById(id);
		}else{
			products = new Products();
			products.setGroupid(groupId);
		}
		model.addAttribute("products", products);
		return "/ext/products/productsForm";
	}
	
	
	@RequestMapping(value = "save", method = RequestMethod.POST)
	public  @ResponseBody  String save(Products products) {
		productsManager.saveProducts(products);
		return "success";
	}
	
	@RequestMapping(value = "delproductss", method = RequestMethod.POST)
	public @ResponseBody String delproductss(String[] chk) throws IOException {
		productsManager.deleteProductss(chk);
		return ""+chk.length;
	}
	
//	@RequestMapping(value = "alsdgjkfa", method = RequestMethod.POST)
//	public @ResponseBody String alsdgjkfa(HttpServletRequest request) throws IOException {
//		List list = productsManager.findAll();
//		String savePath=request.getSession().getServletContext().getRealPath("/");
//		for(int i =0;i<list.size();i++){
//			Map map = (Map)list.get(i);
//			String picpath=(String)map.get("picpath");
//			String uuid =  new GUID().toString();
//			if(picpath!=null && !"".equals(picpath.trim())){
//				int pos;
//				String ext="";
//		        if((pos = picpath.lastIndexOf('.'))!=(-1)) {
//		            ext = picpath.substring(pos).trim();
//		        }
//				String newFileName = picpath.substring(0,picpath.lastIndexOf("/")+1)+"p_"+uuid+ext;
//	        	String smallFileName=picpath.substring(0,picpath.lastIndexOf("/")+1)+"p_small_"+uuid+ext;
//	        	File f1 = new File(savePath+picpath);
//	        	File f2 = new File(savePath+newFileName);
//	        	f1.renameTo(f2);
//	        	ScaleImage is = new ScaleImage();   
//	            try {   
//	                is.saveImageAsJpg(savePath+newFileName, savePath+smallFileName,200);   
//	            } catch (Exception e) {   
//	                e.printStackTrace();   
//	            }
//	            Products p = productsManager.findById((String)map.get("id"));
//	            p.setPicpath(newFileName);
//	            p.setSmall_picpath(smallFileName);
//	            productsManager.updateProducts(p);
//			}
//		}
//		return "success";
//	}
}

